package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cjgl.entity.Course;
import service.CourseService;

/**
 * Servlet implementation class CourseServlet
 */
@WebServlet("/course")
public class CourseServlet extends HttpServlet {
	private static final long serialVersionUID = 1L; 
	private CourseService courseService=new CourseService();
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		String method =request.getParameter("method");
		if(method==null) {
			list(request,response);
		}else if("update".contentEquals(method)) {
			updete(request,response);
		}else if("save".contentEquals(method)) {
			sava(request,response);
		}else if("delete".contentEquals(method)) {
			delete(request, response);
		}else if("add".contentEquals(method)) {
			add(request, response);
		}
		
		
	}

	private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.getRequestDispatcher("jsp/keCheng/edit.jsp").forward(request, response);
		
	}

	private void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr =request.getParameter("id");
		Integer id=Integer.valueOf(idStr);
		courseService.delete(id);
		response.sendRedirect("course");
	}

	private void sava(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String idStr =request.getParameter("id");
		Integer id=null;
		if(idStr!=null&&!"".contentEquals(idStr)) {
			id=Integer.parseInt(idStr);
		}
		String name = request.getParameter("name");
		String classHour = request.getParameter("classHour");
		Course course=new Course(id,name,classHour);
		courseService.sava(course);
		response.sendRedirect("course");
	}

	private void updete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr =request.getParameter("id");
		Integer id=Integer.parseInt(idStr);
		Course model = courseService.findById(id);
		request.setAttribute("model", model);
		request.getRequestDispatcher("jsp/keCheng/edit.jsp").forward(request, response);
	}

	private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Course> list = courseService.findAll();
		request.setAttribute("list", list);
		request.getRequestDispatcher("jsp/keCheng/list.jsp").forward(request, response);
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

